package controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.DeptInf;
import pojo.UserInf;
import service.DeptInfService;
import util.R;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 部门apid
 */
@RestController
@RequestMapping("/deptInf")
public class DeptInfApi {
    //引入业务层
    @Resource(name = "deptService")
    private DeptInfService deptInfService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public DeptInf getById(@PathVariable Serializable id){
       return deptInfService.getById(id);
    }

    /**
     * 带分页的查询
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/{current}/{size}")
    public Page getList(@PathVariable int current,@PathVariable  int size){
        Page<DeptInf> page=new Page<DeptInf>(current,size);
        return deptInfService.page(page);
    }

    /**
     * 更具id修改
     * @param deptInf
     * @return
     */
    @PutMapping()
    public R updateById(DeptInf deptInf){
        boolean b = deptInfService.updateById(deptInf);
        return result(b,deptInf);
    }

    /**
     * 添加
     * @param deptInf
     * @return
     */
    @PostMapping()
    public R insert(DeptInf deptInf){
        boolean b = deptInfService.save(deptInf);
        return result(b,deptInf);
    }

    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Serializable id){
        boolean b = deptInfService.removeById(id);
        return result(b,id);
    }

    public R result(boolean b,Object data){
        if(b){
            return R.ok().setData(data);
        }else {
            return R.error();
        }
    }
}
